<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/CogDataSet">
		<xsl:for-each select="TableInfo">
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[<xsl:value-of select="Schema"/>].[<xsl:value-of select="ManagedTable"/>_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [<xsl:value-of select="Schema"/>].[<xsl:value-of select="ManagedTable"/>_Delete]
GO

CREATE PROCEDURE [<xsl:value-of select="Schema"/>].[<xsl:value-of select="ManagedTable"/>_Delete]
(
	<xsl:for-each select="ColumnInfo[IsPrimaryKey='true']">@<xsl:value-of select="Column"/><xsl:text> </xsl:text><xsl:value-of select="DataType"/><xsl:if test="not(position()=last())">,
	</xsl:if></xsl:for-each>
)
AS
BEGIN
DELETE FROM [<xsl:value-of select="Schema"/>].[<xsl:value-of select="Table"/>] 
WHERE <xsl:for-each select="ColumnInfo[IsPrimaryKey='true']">[<xsl:value-of select="Column"/>] = @<xsl:value-of select="Column"/><xsl:if test="not(position()=last())"> AND
</xsl:if>
</xsl:for-each>
END
GO
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
